package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.CommonAdapter;
import com.muqi.app.qlearn.adapter.CommonViewHolder;
import com.muqi.app.qlearn.modles.VedioCategory;
import com.muqi.app.qlearn.modles.VedioInfo;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.id;
import com.muqi.app.qlearn.student.R.layout;
import com.muqi.app.qlearn.student.R.menu;
import com.muqi.app.user.widget.SelectableRoundedImageView;
import com.muqi.app.widget.pulltorefresh2.XScrollView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class VedioListActivity extends BaseFragmentActivity implements
XScrollView.IXScrollViewListener ,OnItemClickListener {
	private XScrollView mScrollView;
	private GridView gv;
	private List<VedioInfo> listData = new ArrayList<VedioInfo>();
	private int page = 1;
	private SelectableRoundedImageView sv;
	private TextView tv;
	private String baseUrl = "media/tv/img/pc_test/";
	
	private String id_code;
	private int indexPage;
	
	private RelativeLayout root;
	private LinearLayout.LayoutParams params;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vedio_list);
	}

	@Override
	protected void onInit() {
		
		id_code=getIntent().getStringExtra("id_code");
		indexPage=getIntent().getIntExtra("indexPage", 0);
		
		mScrollView = (XScrollView) findViewById(R.id.xMyScrollView);
		mScrollView.setPullRefreshEnable(true);
		mScrollView.setPullLoadEnable(false);
		mScrollView.setAutoLoadEnable(true);
		mScrollView.setIXScrollViewListener(this);
		mScrollView.setRefreshTime("刚刚");
		View content = LayoutInflater.from(this).inflate(
				R.layout.scroll_content_vedio, null);
		gv = (GridView) content.findViewById(R.id.gv);
		mScrollView.setView(content);
		gv.setAdapter(adapter);
		gv.setOnItemClickListener(this);
	}
	@Override
	protected void onData() {
	
		loadData();

	}

	/**
	 * 加载网络数据
	 */
	private void loadData() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("page", indexPage);
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("id_code", id_code);
			String requestUrl =NetWorkApi.SERVER_IP + ParamsUtils.buildParams(
					NetWorkApi.GET_VIDEO_LIST_API, sendMap);
			MyAsyncHttp.getClient().get(requestUrl,
					new JsonHttpResponseHandler() {

						@Override
						public void onFailure(int statusCode, Header[] headers,
								String responseString, Throwable throwable) {
							super.onFailure(statusCode, headers,
									responseString, throwable);
							//Log.e("===", responseString+"error");

						}

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								JSONObject response) {
						
							//Log.e("===", response.toString());
							mScrollView.stopRefresh();
							mScrollView.stopLoadMore();
							mScrollView.setRefreshTime("刚刚");

							try {
								JSONArray array = response
										.getJSONArray("vedios");
								if (array != null) {
									List<VedioInfo> temps = ResponseUtils
											.getVedioInfos(mContext,
													array.toString());
									//Log.e("===", temps + "  ");
									if (temps != null && temps.size() > 0) {
										listData.clear();
										listData = temps;
										adapter.setListData(listData);
										adapter.notifyDataSetChanged();
										/*if (page == 1) {
											
										} else {
											for (VedioInfo item : temps) {
												listData.add(item);
											}
											adapter.notifyDataSetChanged();
										}

										if (temps.size() < 10) {
											mScrollView
													.setPullLoadEnable(false);
										} else {
											mScrollView.setPullLoadEnable(true);
											page++;
										}*/
									}
								}

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}

					});

		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}

	}

	public void onBack(View view) {
		finish();
	}

	private CommonAdapter<VedioInfo> adapter =new CommonAdapter<VedioInfo>(this, listData, R.layout.item_video) {
		
		@Override
		protected void convertView(View item, VedioInfo t) {
			sv = CommonViewHolder.get(item, R.id.sv_pic);
			tv = CommonViewHolder.get(item, R.id.tv_name);
			root=CommonViewHolder.get(item, R.id.root);
			String url = NetWorkApi.IMA_IP + baseUrl
					+ t.pic;
			params=	(android.widget.LinearLayout.LayoutParams) root.getLayoutParams();
			params.weight=ScreenUtils.getScreenW(VedioListActivity.this)/2;
			params.height=ScreenUtils.getScreenW(VedioListActivity.this)/2*9/16;
			root.setLayoutParams(params);
			Glide.with(mContext).load(url).into(sv);

			tv.setText(t.danyuan_mingcheng);
			
		}
	};
	@Override
	public void onRefresh() {
		page = 1;
		loadData();

	}

	@Override
	public void onLoadMore() {
		loadData();

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent=new Intent(mContext,VedioPlayActivity.class);
		intent.putExtra("vedioInfo", listData.get(arg2));
		startActivity(intent);
		
	}}